package com.wmz.crm.workbench.controller;

import com.wmz.crm.commons.constants.Constants;
import com.wmz.crm.commons.entity.ReturnObject;
import com.wmz.crm.settings.entity.DicValue;
import com.wmz.crm.settings.entity.User;
import com.wmz.crm.settings.service.DicValueService;
import com.wmz.crm.settings.service.UserService;
import com.wmz.crm.workbench.entity.*;
import com.wmz.crm.workbench.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

@Controller
public class TranController {
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private UserService userService;
    @Autowired
    private TranService tranService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ContactsService contactsService;
    @Autowired
    private TranHistoryService tranHistoryService;
    @Autowired
    private TranRemarkService tranRemarkService;

    @RequestMapping("/workbench/transaction/index.do")
    public String index(HttpServletRequest request){
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");
        List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");
        List<DicValue> transactionTypeList = dicValueService.queryDicValueByTypeCode("transactionType");

        //把数据保存到request域中
        request.setAttribute("stageList",stageList);
        request.setAttribute("transactionTypeList",transactionTypeList);
        request.setAttribute("sourceList",sourceList);
        return "workbench/transaction/index";
    }

    @RequestMapping("/workbench/transaction/save.do")
    public String save(HttpServletRequest request){
        List<User> userList = userService.queryAllUsers();
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");
        List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");
        List<DicValue> transactionTypeList = dicValueService.queryDicValueByTypeCode("transactionType");

        //把数据保存到request域中
        request.setAttribute("userList",userList);
        request.setAttribute("stageList",stageList);
        request.setAttribute("transactionTypeList",transactionTypeList);
        request.setAttribute("sourceList",sourceList);
        return "workbench/transaction/save";
    }

    @RequestMapping("/workbench/transaction/queryTranByConditionForPage.do")
    @ResponseBody
    public Object queryTranByConditionForPage(@RequestParam Map<String,Object> map){
        map.replace("beginNo",Integer.parseInt(map.get("beginNo").toString()));
        map.replace("pageSize",Integer.parseInt(map.get("pageSize").toString()));
        //调用service方法
        List<Tran> tranList = tranService.queryTranByConditionForPage(map);
        int totalRows = tranService.queryCountOfTranByCondition(map);
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("tranList",tranList);
        retMap.put("totalRows",totalRows);
        return retMap;
    }

    @RequestMapping("/workbench/transaction/getPossibilityFromProperties.do")
    @ResponseBody
    public Object getPossibilityFromProperties(String stage){
        ResourceBundle possibility = ResourceBundle.getBundle("possibility");
        String stageValue = possibility.getString(stage);
        return stageValue;
    }
    @RequestMapping("/workbench/transaction/queryCustomerNameByName.do")
    @ResponseBody
    public Object queryCustomerNameByName(String name) {
        //调用service层方法
        List<String> customerList = customerService.queryCustomerNameByName(name);
        return customerList;
    }
    @RequestMapping("/workbench/transaction/queryActivityByName.do")
    @ResponseBody
    public Object queryActivityByName(String name) {
        ReturnObject returnObject = new ReturnObject();
        //调用service层方法
        List<Activity> activityList = activityService.queryActivityByName(name);
        if(activityList != null && activityList.size() > 0) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetObj(activityList);
        }else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
        }
        return returnObject;
    }
    @RequestMapping("/workbench/transaction/queryContactsByName.do")
    @ResponseBody
    public Object queryContactsByName(String name) {
        ReturnObject returnObject = new ReturnObject();
        //调用service层方法
        List<Contacts> contactsList = contactsService.queryContactsByName(name);
        if(contactsList != null && contactsList.size() > 0) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetObj(contactsList);
        }else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
        }
        return returnObject;
    }
    @RequestMapping("/workbench/transaction/saveCreateTran.do")
    @ResponseBody
    public Object saveCreateTran(@RequestParam Map<String,Object> map, HttpSession session) {
        map.put(Constants.SESSION_USER,session.getAttribute(Constants.SESSION_USER));
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        try {
            //调用service层方法
            tranService.saveCreateTran(map);
        }catch (Exception e) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试……");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/transaction/detailTran.do")
    public String detailTran(String id,HttpServletRequest request){
        //调用service层方法
        Tran tran = tranService.queryTranForDetailById(id);
        List<TranRemark> tranRemarkList = tranRemarkService.queryTranRemarkForDetailByTranId(id);
        List<TranHistory> tranHistoryList = tranHistoryService.queryTranHistoryForDetailByTranId(id);
        List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");
        //获取possibility
        ResourceBundle bundle = ResourceBundle.getBundle("possibility");
        tran.setPossibility(bundle.getString(tran.getStage()));
        //把数据保存到request域中
        request.setAttribute("tran",tran);
        request.setAttribute("tranRemarkList",tranRemarkList);
        request.setAttribute("tranHistoryList",tranHistoryList);;
        request.setAttribute("stageList",stageList);
        return "workbench/transaction/detail";
    }
}
